//
//  SchoolViewController.m
//  hellomui
//
//  Created by zhangtao on 15/10/17.
//  Copyright © 2015年 zt.td. All rights reserved.
//

#import "SchoolViewController.h"

#define COL [UIColor colorWithRed:23/255.f green:170/255.f blue:71/255.f alpha:1]
@interface SchoolViewController ()
@property (strong, nonatomic) IBOutlet UIButton *littleSchoolBtn;
@property (strong, nonatomic) IBOutlet UIButton *middleSchoolBtn;
@property (strong, nonatomic) IBOutlet UIButton *hightSchoolBtn;
@property (weak, nonatomic) IBOutlet UILabel *l;
@property (weak, nonatomic) IBOutlet UILabel *m;
@property (weak, nonatomic) IBOutlet UILabel *h;

@property (strong, nonatomic) IBOutlet UITableView *listTable;
@property(nonatomic,strong)NSMutableArray *littleArray;
@property(nonatomic,strong)NSMutableArray *middleArray;
@property(nonatomic,strong)NSMutableArray *hightArray;
@property(nonatomic,strong)NSMutableArray *schoolArray;

@property(nonatomic,assign)NSInteger selectCell;
@property(nonatomic,strong)UIImageView *imageIV;

@end

@implementation SchoolViewController
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    
    view.backgroundColor = VIEWCO;//[UIColor clearColor];
    
    [tableView setTableFooterView:view];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.listTable.backgroundColor = [UIColor clearColor];//VIEWCO;
    self.title = @"学校";
    _l.backgroundColor = _littleSchoolBtn.tintColor = COL;
    _middleSchoolBtn.tintColor = _hightSchoolBtn.tintColor = [UIColor blackColor];
    [self setExtraCellLineHidden:_listTable];
    
     self.automaticallyAdjustsScrollViewInsets = false;
    // Do any additional setup after loading the view from its nib.
    _selectCell = -1;
    
    _listTable.dataSource = self;
    _listTable.delegate = self;
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    NSUSER(def);
    if ([def valueForKey:@"shengID"]&&[def valueForKey:@"shiID"]&&[def valueForKey:@"areaID"]) {
        [self getSchoolSheng:[def valueForKey:@"shengID"] andShiId:[def valueForKey:@"shiID"] andAreaID:[def valueForKey:@"areaID"]];
    }
    
}

-(void)initColor{
    _l.backgroundColor = _m.backgroundColor = _h.backgroundColor =[UIColor clearColor];
    _littleSchoolBtn.tintColor = _hightSchoolBtn.tintColor = _middleSchoolBtn.tintColor = [UIColor blackColor];
}



- (IBAction)littleSchoolBtn:(id)sender {
    [self initColor];
    _littleSchoolBtn.tintColor = _l.backgroundColor = COL;
    _schoolArray = _littleArray;
    [_listTable reloadData];
}

- (IBAction)middleSchoolBtn:(id)sender {
    [self initColor];
    _middleSchoolBtn.tintColor = _m.backgroundColor = COL;
    _schoolArray = _middleArray;
    [_listTable reloadData];

}

- (IBAction)hightSchoolbBtn:(id)sender {
    [self initColor];
    _hightSchoolBtn.tintColor = _h.backgroundColor = COL;
    _schoolArray = _hightArray;
    [_listTable reloadData];

}



-(void)getSchoolSheng:(NSString *)shengid andShiId:(NSString *)shiid andAreaID:(NSString *)areaid{
    NSDictionary *dic = @{@"shengID":shengid,@"shiID":shiid,@"areaID":areaid};
  NSString *path = @"personalCenter/getSchool";
    [[HttpConnctionManager sharedInstance]PostRequestParam:dic andUrl:path andSuccessedArrayBlock:^(NSArray *succeedResult) {
        NSLog(@"%@",succeedResult);
        
        
        _littleArray = [NSMutableArray array];
        _middleArray = [NSMutableArray array];
        _hightArray = [NSMutableArray array];
        _schoolArray = [NSMutableArray array];
        
        for (NSDictionary *dic in succeedResult) {
            
            NSString *type = [dic valueForKey:@"type"];
            NSString *schoolName = [dic valueForKey:@"name"];
            
            if ([type isEqualToString:@"小学"]) {
                [_littleArray addObject:schoolName];
                
            }else if ([type isEqualToString:@"初中"]){
                [_middleArray addObject:schoolName];
            }else{
                [_hightArray addObject:schoolName];
            }
            
        }
        _schoolArray = _littleArray;
        [_listTable reloadData];
        
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        NSLog(@"error::%@",error);
    }];
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//   
//        UIView *headIV = [[UIView alloc]init];
//       
//        return headIV;
//    
//}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _schoolArray.count?_schoolArray.count:0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    
        cell.textLabel.text = _schoolArray[indexPath.row];
    if (_selectCell == indexPath.row) {
        _imageIV.image = [UIImage imageNamed:@"item_selected.png"];
      
        [cell addSubview:_imageIV];
    }
    
    if (indexPath.row == 0 ) {
        UILabel *topL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, 1)];
        topL.backgroundColor = RGB(215, 215, 215, 1);
        [cell addSubview:topL];
        
    }else if(indexPath.row == (_schoolArray.count-1)) {
        UILabel *topL = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, MAIN_WIDTH, 1)];
        topL.backgroundColor = RGB(220, 220, 220, 1);
        [cell addSubview:topL];
        
        cell.layer.borderWidth = 0;
        cell.layer.borderColor = [[UIColor redColor]CGColor];//[RGB(210, 210, 210, 1)CGColor];
        UILabel *headL = [[UILabel alloc]initWithFrame:CGRectMake(0, 39, MAIN_WIDTH, 1)];
        headL.backgroundColor = RGB(215, 215, 215, 1);
        [cell addSubview:headL];
    }else{
        UILabel *topL = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, MAIN_WIDTH, 1)];
        topL.backgroundColor = RGB(220, 220, 220, 1);
        [cell addSubview:topL];
    }
    
    
    
        return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _selectCell = indexPath.row;
    
    _change.schoolLabel.text = _schoolArray[indexPath.row];
    NSUSER_DEF_NORSET(_change.schoolLabel.text, @"SCHOOL");
    [_imageIV removeFromSuperview];
    _imageIV = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, 5, 25, 25)];
    [_listTable reloadData];
    [self request];
    [self.navigationController popViewControllerAnimated:YES];

}

-(void)request{
    NSString *path = @"personalCenter/updateReadInfo";
    NSString *userId = [NSString stringWithFormat:@"%@",NSUSER_DEF(@"USERID")];
      
    NSDictionary *dic = @{@"userId":userId,@"schoolName":[NSString stringWithFormat:@"%@",NSUSER_DEF(@"SCHOOL")]};
    NSDictionary *dics =  @{@"userId":userId,@"schoolName":[NSString stringWithFormat:@"%@",NSUSER_DEF(@"SCHOOL")],@"bd_sig":[ToolObjc md5Str:dic]};
    
    [[HttpConnctionManager sharedInstance]requestParam:dics andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        NSLog(@"%@%@",[succeedResult valueForKey:@"resCode"],[succeedResult valueForKey:@"resMessage"]);
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        
    }];
    
    

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
